<?php
/**
 * @link https://ym2.cleverstone.top
 * @copyright Copyright (c) 2020 Yii Manager Software LLC
 */

namespace builder\helper;

use Yii;
use yii\di\Instance;
use yii\helpers\ArrayHelper;
use yii\db\QueryInterface;
use builder\widget\ajax\LinkPager;

/**
 * 应用
 * @author cleverstone
 * @since ym2.0
 */
class App
{
    /**
     * 获取应用程序参数
     * @param null $key
     * @param null $default 默认值
     * @return mixed
     */
    public static function params($key = null, $default = null)
    {
        $defaultParams = require dirname(__DIR__) . '/config/default.php';
        $appParams = Yii::$app->params;

        $params = ArrayHelper::merge($defaultParams, $appParams);
        if (!empty($key)) {
            return isset($params[$key]) ? $params[$key] : $default;
        }

        return $params;
    }

    /**
     * 系统左侧菜单
     * @param array $options
     * @return string
     */
    public static function menu($options = [])
    {
        $widgetClass = App::params('admin.navMenu');
        if (class_exists($widgetClass)) {
            return $widgetClass::widget($options);
        }

        return '';
    }

    /**
     * 系统顶部导航
     * @param array $options
     * @return string
     */
    public static function nav($options = [])
    {
        $widgetClass = App::params('admin.topNav');
        if (class_exists($widgetClass)) {
            return $widgetClass::widget($options);
        }

        return '';
    }

    /**
     * RBAC管理器
     * @return \builder\base\RbacValidateInterface
     */
    public static function rbacManager()
    {
        $rbacManager = App::params('admin.rbacManager');
        return Instance::ensure($rbacManager, 'builder\base\RbacValidateInterface');
    }

    /**
     * 解析分页
     * @param QueryInterface $query Db query instance
     * @param null|\Closure $callback A callable or closure
     * @return array
     * @throws \Throwable
     */
    public static function resolvePage(QueryInterface $query, $callback = null)
    {
        $countQuery = clone $query;
        $pagination = new \yii\data\Pagination([
            'totalCount' => $countQuery->count(),
            'pageSizeLimit' => [1, 500],
        ]);
        $pageStr = LinkPager::widget([
            'pagination' => $pagination,
        ]);

        $query->offset($pagination->offset)->limit($pagination->limit);
        $data = $query->all();
        if (is_callable($callback)) {
            $data = call_user_func($callback, $data);
        }

        return [$pageStr, $data];
    }

    /**
     * 分页查询
     * @param QueryInterface $query Query db instance
     * @return \stdClass
     */
    public static function queryPageAll(QueryInterface $query)
    {
        $countQuery = clone $query;
        $pagination = new \yii\data\Pagination([
            'totalCount' => $countQuery->count(),
            'pageSizeLimit' => [1, 50],
        ]);
        $result = [
            'data' => $query->offset($pagination->offset)->limit($pagination->limit)->all(),
            'pagination' => $pagination,
        ];
        return (object)$result;
    }

    /**
     * 从表单数据中获取文件路径
     * @param array $bodyParams 表单数据
     * @param string $paramsKey 参数字段
     * @return array|string
     */
    public static function getFileLinkFromParams($bodyParams, $paramsKey)
    {
        if (empty($bodyParams) || empty($bodyParams[$paramsKey])) {
            return '';
        }

        $fileArray = $bodyParams[$paramsKey];
        $fileCount = count($fileArray);
        $linkArray = ArrayHelper::getColumn($fileArray, 'link');
        if ($fileCount == 1) {
            return reset($linkArray);
        } else {
            return $linkArray;
        }
    }
}